﻿namespace JunFan.Mapping
{
    using System;
    using AutoMapper;

    public class DateTimeNullableConverter : ITypeConverter<DateTime, DateTime?>
    {
        public DateTime? Convert(ResolutionContext context)
        {
            return ConvertDateTimeNullable((DateTime) context.SourceValue);
        }

        private static DateTime? ConvertDateTimeNullable(DateTime source)
        {
            return source == default(DateTime)
                       ? (DateTime?)null
                       : source;
        }
    }
}